<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/20
 * Time: 22:34
 */
class Myself extends MY_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->loadHelper("url");
    }

    //显示个人信息
    public function index()
    {

        $this->loadModel("userinfo");

        $sess_user = $this->session("user");
        $db_users = $this->userinfo->getWhere(["id"=>$sess_user->id]);

        $db_user = reset($db_users); //信息表


        $data_arr = ['db_user'=>$db_user,'sess_user'=>$sess_user];


        $this->loadView(ADMIN.'Myself/myself_index',$data_arr);
    }

    public function upload_icon()
    {
        $form_file_name = 'icon';
        if(empty($_FILES[$form_file_name]))
        {
           echo json_encode(['code'=>0,'message'=>'上传错误']);
        }
        else
        {
            $upload_base_path = './public/user/icon/';
            $file_base_url = substr($upload_base_path,2);
            $config['upload_path'] = $upload_base_path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '600';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config["encrypt_name"]=true;
            $res= $this->upload_file($config,"icon");

            if(!is_array($res))
            {
                echo json_encode(['code'=>0,'message'=>'Error message:'.$res]);
            }
            else{
                $icon_addr = config_item("base_url").$file_base_url.$res[$form_file_name]['file_name'];

                $this->loadModel("userinfo");
                $set['icon_addr'] = $icon_addr;
                $user_id = $this->session('user')->id;
                $this->userinfo->update($set,['id'=>$user_id]);

                echo json_encode(['code'=>1,'message'=>$icon_addr]);
            }
        }

    }

    //private function _m

    //修改自己
    public function base_info()
    {
        $type = $this->post("type");
        $this->loadModel("userinfo");
        $user_id = $this->session("user")->id;
        if($type=="update")
        {
            $nick_name = $this->post("nick_name");
            $address = $this->post("address");
            $birthday = $this->post("birthday");
            $user_job = $this->post("job");
            $github_addr = $this->post("github_addr");
            $resume = $this->post("resume");
            $data = compact('nick_name','address','birthday','user_job','github_addr','resume');

            $res = $this->userinfo->update($data,['id'=>$user_id]);

            $db_user = $this->userinfo->getById($user_id);

            echo json_encode($db_user);
        }
        else if($type=="update_gender")
        {
            $gender = $this->post("gender");
            $data = compact('gender');

            $res = $this->userinfo->update($data,['id'=>$user_id]);
        }else if($type=="update_con_info")
        {
           $qq_num = $this->post("qq_num");
           $user_email = $this->post("user_email");

           $data = compact('qq_num','user_email');
           $res = $this->userinfo->update($data,['id'=>$user_id]);

           $obj = $this->userinfo->getById($user_id);

           echo json_encode($obj);
        }else if($type=="update_edu_info")
        {
            $edu_info = $this->post("edu_info");
            $data=compact('edu_info');
            $res = $this->userinfo->update($data,['id'=>$user_id]);

            $obj = $this->userinfo->getById($user_id);

            echo json_encode($obj);
        }
    }


    //重置密码
    public function safe()
    {
        $error_message = "";
        if($this->isGet())
        {
            $this->loadView(ADMIN.'myself/myself_safe',compact('error_message'));
        }
        else
        {
            try {

                 //获取数据
                $old_password = $this->post("old_password");
                $password = $this->post("password");


                //验证表单
                $this->loadLibrary("form_validation");

                $this->form_validation->set_error_delimiters('', '\\');

                if ($this->form_validation->run() === false) {

                    throw new Exception(validation_errors());
                } else {

                    //数据库查询
                    $this->load->model("user");

                    $sess_user = $this->session("user");
                    $db_users = $this->user->getWhere(['id' => $sess_user->id]);
                    $db_user = reset($db_users);

                    if (md5($old_password) == $db_user->pass_word) //原密码正确
                    {

                        //重置密码
                        $update_psw = ['pass_word' => md5($password)];
                        $update_result = $this->user->update($update_psw, ['id' => $db_user->id]);

                        if ($update_result)//密码重置成功
                        {
                            //写入操作事件日志

                            $this->loadModel("usereventinfo");
                            $res = $this->usereventinfo->add_event_log(UserEventInfo::EVENT_RESET_PSW,$this->time());

                            throw new Exception("密码重置成功!");
                        } else {
                            throw new Exception("不能重置为原密码!");
                        }
                    } else {
                        throw new Exception("原密码输入错误!");
                    }
                }
            }catch(Exception $e)
            {
                $error_message = $e->getMessage();
                $this->loadView(ADMIN.'myself/myself_safe',compact('error_message'));
            }

        }

    }

    public function log()
    {

        $this->loadModel("usereventinfo");
        $user_id = $this->session("user")->id;
        $offset = $this->uri->segment(4);
        $base_url = web_url('myself/log',true);
        $where = ['user_id'=>$user_id];
        $event_logs = $this->usereventinfo->getWhere($where,$offset,self::PER_PAGE,true);
        $count = $this->usereventinfo->getRows($where);
        $views = [ADMIN.'myself/myself_log'];
        $view_data['logs'] = $event_logs;
        $this->_pagination($base_url,$count,self::PER_PAGE,$views,$view_data);

    }

    public function logKey()
    {
        $this->loadModel("usereventinfo");
        $offset = $this->uri->segment(5)??0;

        $base_url = "myself/logkey/";
        $data = $this->_search_key_word($base_url,"ip_addr");
        $where = $data['where'];
        $base_url = $data['base_url'];
        $count = $this->usereventinfo->getRows($where);
        $event_logs = $this->usereventinfo->getWhere($where,$offset,self::PER_PAGE,true);
        $views = [ADMIN.'myself/myself_log'];
        $view_data['logs'] = $event_logs;
        $this->_pagination($base_url,$count,self::PER_PAGE,$views,$view_data);
    }

    public function logTime()
    {
        $this->loadModel("usereventinfo");

        $offset = $this->uri->segment(6)??0;

        $base_url = 'myself/logtime/';
        $data = $this->_time_search($base_url);
        $where  = $data['where'];
        $base_url = $data['base_url'];
        $count = $this->usereventinfo->getRows($where);
        $event_logs = $this->usereventinfo->getWhere($where,$offset,self::PER_PAGE,true);
        $views = [ADMIN.'myself/myself_log'];
        $view_data['logs'] = $event_logs;
        $this->_pagination($base_url,$count,self::PER_PAGE,$views,$view_data);
    }



    public function about()
    {
        $this->loadModel("userinfo");

        $user_id = $this->session("user")->id;

        if($this->isGet())
        {

            $about_obj = $this->userinfo->getById($user_id,true);

            $data['about'] = $about_obj->about;
            $this->loadView(ADMIN.'myself/myself_about',$data);
        }
        else{

            $about_content = $this->post("about");
            $data = ['about'=>$about_content];

            $this->userinfo->update($data,['id'=>$user_id]);

            $about_obj = $this->userinfo->getById($user_id,true);

            echo $about_obj->about;
        }
    }

    public function message()
    {
        $this->loadView(ADMIN.'myself/myself_message');
    }
}